package com.hnevc.androidui3;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.util.List;

public class CoursesActivity extends AppCompatActivity {
    //保存读取到的json数据
    List<Course> courseList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_courses);

        //要将数据呈现到ListView中
        initData();
        initView();

    }

    private void initView() {
        //获取listView控件
        ListView lvCourses = findViewById(R.id.lv_courses);
        //创建适配器对象
        CourseAdapter adapter = new CourseAdapter();
        //将适配器绑定到控件
        lvCourses.setAdapter(adapter);
    }

    private void initData() {
        //从json文件中解析数据
        Gson gson = new Gson();

        Type listType = new TypeToken<List<Course>>() {
        }.getType();
        //获取data.json的输入流
        InputStream in = getResources().openRawResource(R.raw.data);
        //将输入转换为reader
        Reader reader = new InputStreamReader(in);
        //将reader读取json字符，转换为listType类型的链表
        courseList = gson.fromJson(reader, listType);
    }

    class CourseAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return courseList.size();//获取要显示的数据项数
        }

        @Override
        public Object getItem(int position) {
            return courseList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //导入布局到视图
            View view = View.inflate(
                    CoursesActivity.this
                    , R.layout.list_item,
                    null);
            //从list_item中获取图片控件，并设置控件显示的图片
            ImageView imageView = view.findViewById(R.id.iv_course);

            final Course course = courseList.get(position);//获取当前位置的课程

            if (course.getCourseImage().equals("cc.png")) {
                imageView.setImageResource(R.drawable.cc);
            } else if (course.getCourseImage().equals("java.png")) {
                imageView.setImageResource(R.drawable.java);
            }else if (course.getCourseImage().equals("os.png")){
                imageView.setImageResource(R.drawable.os);
            }


            TextView tvCourseName = view.findViewById(R.id.tv_course_name);
            tvCourseName.setText(course.getCourseName());

            //**加事件
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(CoursesActivity.this, course.getCourseName(), Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(CoursesActivity.this, MainActivity.class);
                    startActivity(intent);
                    CoursesActivity.this.fileList();
                }
            });

            return view;
        }
    }
}
